Library.WebControl.DragDrop = (function() {
    var originalObject;
    var cloneObject;
    var offset;
    function GetCloneContainer() {
        var id = 'Library.WebControl.DragDrop.CloneContainerDivId';
        var c = document.getElementById(id);
        if (!c) {
            c = document.createElement('div');
            c.setAttribute('id', id);
//            if(c.style.setAttribute)
//            c.style.setAttribute("position", "absolute")
//            else
            c.style.position = 'absolute';
            document.getElementsByTagName('body')[0].appendChild(c);
        }
        return c;
    };
    function HandleMouseDown(e, container) {
        var evt = Library.WebControl.Util.GetEvent(e);
        if (evt.which) {
            mouseButton = evt.which;
        }
        else {
            mouseButton = evt.button;
        }
        if (mouseButton == 1) {
            document.onmousemove = HandleMouseMove;
            document.onmouseup = HandleMouseUp;
        }
        var objPos = Library.WebControl.Util.GetPosition(container);
        var absParrent = Library.WebControl.Util.GetAbsParrent(container);
        if (absParrent != null) {
						var posAbsParrent = Library.WebControl.Util.GetPosition(absParrent);
						objPos.X = objPos.X - posAbsParrent.X;
						objPos.Y = objPos.Y - posAbsParrent.Y;
        }
        var mosPos = Library.WebControl.Util.GetMousePosition(e);
        offset = [mosPos.X - objPos.X, mosPos.Y - objPos.Y];
        if (originalObject == container) {
            return false;
        }
        originalObject = container;
        container.style.position = 'absolute';
        if (container.getAttribute('cpwm') != 't') {
            cloneObject = container;
        }
        else {
            cloneObject = container.cloneNode(true);
            var c = GetCloneContainer();
            while (c.childNodes.length > 0) {
                c.removeChild(c.childNodes[0]);
            }
            
            c.style.top = objPos.Y + 'px';
            c.style.left = objPos.X + 'px';
            cloneObject.style.top = '0px';
            cloneObject.style.left = '0px';
            c.appendChild(cloneObject);
            cloneObject = c;
        }
        // cancel out any text selections
        document.body.focus();

        // prevent text selection in IE
        document.onselectstart = function () { return false; };
        // prevent IE from trying to drag an image
        //target.ondragstart = function() { return false; };
        
        // prevent text selection (except IE)
        return false;
    };

    function HandleMouseMove(e) {
        var evt = Library.WebControl.Util.GetEvent(e);
        MoveObject(evt);
        Library.WebControl.DragDrop.MouseMove(originalObject, evt);

    };
    function HandleMouseUp(e) {
        var evt = Library.WebControl.Util.GetEvent(e);
        document.onmousemove = null;
        document.onmouseup = null;        
        document.onselectstart = null;
        Library.WebControl.DragDrop.MouseUp(cloneObject, evt);
    };
    function MoveObject(e) {
        var mosPos = Library.WebControl.Util.GetMousePosition(e);
        var x = mosPos.X - offset[0];
        var y = mosPos.Y - offset[1];
        cloneObject.style.top = y + 'px';
        cloneObject.style.left = x + 'px';
        //if (document.selection) {
         //   document.selection.clear();
        //}
        

    };
    return {
        Register: function(obj, container, copyWhenMoved) {
            if (typeof (obj) == 'string') {
                obj = Library.WebControl.Util.GetObjectById(obj);
            }
            if (typeof (container) == 'string') {
                container = Library.WebControl.Util.GetObjectById(container);
            }
            if (!obj)
                return;
            obj.onmousedown = function(e) {
                return HandleMouseDown(e, container);
            };
            if (copyWhenMoved) {
                container.setAttribute('cpwm', 't');
            }
            else {
                container.style.position = 'absolute';
            }
            obj.style.cursor = 'move';
        },
        MouseMove: function(s, e) {

        },
        MouseUp: function(s, e) {

        }
    };
} ());



